<?php
/**
 * user: daishiqi
 * Date：2021/6/9
 * Time: 8:28 下午
 */

namespace App\Http\Middleware;


use App\Http\Model\Hint;
use App\Http\Model\User;
use Illuminate\Support\Facades\Redis;
use Closure;

class CheckAccessToken
{
    public function handle($request, Closure $next)
    {

        $token = $request->access_token ?? '';
        if (!$token) {
            return error(Hint::ACCESS_TOKEN_IS_INVALID);
        }

        //检验kk token 是否有效
        $key = User::THIRD_COOPERATE_REDIS_PRE . $token;
        $openId = Redis::get($key);
        if (!Redis::exists($key) || empty($openId)) {
            return error(Hint::ACCESS_TOKEN_IS_INVALID);
        }
        //校验access_token 是否合法
        if ($openId != ($request->open_id ?? '')) {
            return error(Hint::USER_TOKEN_IS_ILLEGAL);
        }
        return $next($request);
    }


}